/**
 * @author LKQ
 * @date 2022/1/8 10:52
 * @description DFS 递归
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode t1 = new TreeNode(1), t3 = new TreeNode(3), t6 = new TreeNode(6),
                t4 = new TreeNode(4, t3, t6), t5 = new TreeNode(5, t1, t4);
        solution.isValidBst(t5);
    }
    public boolean isValidBst(TreeNode root) {
        return isValidBst(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public boolean isValidBst(TreeNode node, long lower, long upper) {
        if (node == null) {
            return true;
        }
        if (node.val <= lower || node.val >= upper) {
            return false;
        }
        return isValidBst(node.left, lower, node.val) && isValidBst(node.right, node.val, upper);
    }

}
